﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ConfApp.Infrastructure;
using ConfApp.Models;
using ConfApp.Services;
using Microsoft.Practices.Prism.Commands;

namespace ConfApp.ViewModels
{
    public class SpeakerListViewModel : ViewModel
    {
        private readonly IConfDataStoreLocator _confDataStoreLocator;

        private ObservableCollection<GroupList<SpeakerInfoViewModel>> _observableSpeakerInfoGroups;
        private CollectionViewSource _speakerInfoGroupsViewSource;

        public SpeakerListViewModel(
            IConfDataStoreLocator confDataStoreLocator,
            INavigationService navigationService)
            : base(navigationService)
        {
            _confDataStoreLocator = confDataStoreLocator;
            Initialize();
        }

        private void Initialize()
        {
            _version = -1;
            Refresh();

            this.SpeakerSelectedCommand = new DelegateCommand<SpeakerInfoViewModel>(
                s =>
                    {
                        NavigationService.Navigate(
                            new Uri("/Views/SpeakerDetailsView.xaml?ID=" + s.ID, UriKind.Relative));
                    });
        }

        private int _version;

        private void Refresh()
        {
            var version = _confDataStoreLocator.GetStore().Version;
            if (version > _version)
            {
                _version = version;
                _observableSpeakerInfoGroups = new ObservableCollection<GroupList<SpeakerInfoViewModel>>();
                var speakerInfoViewModelGroups = GroupSpeakers(_confDataStoreLocator.GetStore().GetSpeakers());

                speakerInfoViewModelGroups.ForEach(_observableSpeakerInfoGroups.Add);

                _speakerInfoGroupsViewSource = new CollectionViewSource {Source = _observableSpeakerInfoGroups};
            }
        }

        private List<GroupList<SpeakerInfoViewModel>> GroupSpeakers(IEnumerable<Speaker> speakers)
        {
            var result = new List<GroupList<SpeakerInfoViewModel>>();
            var groupHeaders = new string[]{"a","b","c","č","ć","d","đ","e","f","g","h","i","j","k","l","lj","m","n","nj","o","p","q","r","s","š","t","u","v","w","x","y","z","ž"};
            var people = speakers.OrderBy(s => s.FullName).Select(s => new SpeakerInfoViewModel(s, NavigationService));
            var groups = new Dictionary<string, GroupList<SpeakerInfoViewModel>>();
            foreach (string header in groupHeaders)
            {
                var group = new GroupList<SpeakerInfoViewModel>(header);
                result.Add(group);
                groups[header] = group;
            }
            foreach (var person in people)
            {
                var fullName = person.FullName.Trim().ToLower();
                var groupName = fullName.Substring(0, 2);
                if (groupName=="lj" || groupName=="nj")
                {
                    groups[groupName].Add(person);
                }
                else
                {
                    groups[fullName.Substring(0, 1)].Add(person);
                }
            }
            return result;
        }

        public ICollectionView SpeakerInfos
        {
            get
            {
                Refresh();
                return _speakerInfoGroupsViewSource.View;
            }
        }

        public DelegateCommand<SpeakerInfoViewModel> SpeakerSelectedCommand { get; set; }

        public override void IsBeingActivated()
        {

        }
    }
}
